define(function(require, exports, module) {
    'use strict';

    var rk = require('rk');
    require('../../../xsyBase');
    var tpl = require('platform/widgets/oa/group/xsyGroupMembers/widget.tpl');

    $.widget('rk.xsyGroupMember', $.rk.xsyBase, {
        _params: {},
        _create: function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            me._beIniting();
            me.bindEventHandler();
        },
        _init: function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            me.reload();
        },

        reload: function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;


            var data = {
                groupId: me._getCommonData().groupId,
                ownerId: me._getCommonData().ownerId,
                belongId: me._getCommonData().belongId
            }
            me._loadData(data, function(result) {
                elem.html('');
                var canUpdate = result.self.canUpdate;

                elem.html(rk.templateText(tpl, $.extend(result.self, {
                    canUpdate: canUpdate,
                    size: result.self.members.length,
                    groupId: data.groupId,
                    ownerId: data.ownerId
                })));
            })
        },

        bindEventHandler: function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            var groupId = me._getCommonData().groupId;
            var belongLabel = me._getCommonData().belongLabel;

            if(!belongLabel){
                belongLabel = rk.queryBelong(me._getCommonData().belongId, 'label');
            }

            elem.addTeamMember({
                selector: 'a.xsy_group_members_add',
                title: rk.i18n('ADD_STAFF_IN_CHARGE'),
                type : 'groupMember',
                url: '/json/sns_group/join.action',
                success: function() {
                    me.reload();
                    afterGroupMemberChange("add")
                }

            });

            function afterGroupMemberChange(type) {
                me._triggerOuterEvent("onGroupMemberChange", {
                    type: type
                });
            }

            rk.delegateMenu({
                container: elem,
                buttonSel: 'a.xsy_group_members_arrow',
                menuHtml: '',
                itemSel: 'a[act]',
                onOpen: function(button, menuSel, menuPos) {
                    var row = button.closest('li.js_xsy_group_member_row');
                    var ownerFlag = row.attr('ownerFlag');
                    $(menuSel).html(rk.templateText(require('platform/widgets/oa/group/xsyGroupMembers/menu/opera_menu.tpl'), {
                        ownerFlag: ownerFlag
                    }))
                    button.find("a.xsy_group_members_arrow,i").addClass('xsyvisible');
                },
                onSelect: function(e, button, itemEl) {
                    var $this = itemEl;
                    var act = $this.attr('act');
                    button.find("a.xsy_group_members_arrow,i").removeClass('xsyvisible');
                    if (act == 'change') {
                        var row = button.closest('li.js_xsy_group_member_row');

                        var uid = row.attr('uid');
                        var ownerFlag = row.attr("ownerFlag") == 1 ? 0 : 1;
                        var name = row.find('span.xsy_group_members_name').html();
                        var groupMembersRole = row.find('span.xsy_group_members_role');

                        var tip;
                        if (ownerFlag) {
                            // tip = $.format('确定要给【{name}】分配修改权限吗？<br/>他将能够修改客户的详细资料', {
                            //     name: name
                            // });
                            tip = rk.i18n('REFER_ASSIGN_MODIFY_PER_TIP',name, belongLabel);
                        } else {
                            // tip = $.format('确定要收回【{name}】的修改权限吗？<br/>他将不再能够修改客户的详细资料', {
                            //     name: name
                            // });
                            
                            tip = rk.i18n('REFER_BACK_MODIFY_PER_TIP',name, belongLabel);
                        }

                        $.confirm(tip, function() {
                            $.postJson('/json/sns_group/changePermission.action', {
                                groupId: groupId,
                                uid: uid,
                                ownerFlag: ownerFlag
                            }).done(function(json) {
                                row.attr('ownerFlag', ownerFlag);
                                if (json.status == 0) {
                                    if (ownerFlag) {
                                        groupMembersRole.html(rk.i18n('REFER_HAVE_MODIFY_PER'));
                                    } else {
                                        groupMembersRole.html("");
                                    }
                                    afterGroupMemberChange("change")

                                } else if (json.status == 300001) {
                                    $.msg(rk.i18n('CRM_CORE_NO_PERMISSION') + rk.i18n('CRM_CORE_ADD') + tempName);
                                } else {
                                    $.msg(rk.i18n('CRM_CORE_ADD') + tempName + rk.i18n('CRM_CORE_FAILED'));
                                }
                            }).fail(function(def, code, message) {
                                def.status != 0 && $.msg(rk.i18n('REQUEST_ERROR'));
                            });
                        });
                    } else if (act == 'remove') {
                        var row = button.closest('li.js_xsy_group_member_row');
                        var uid = row.attr('uid');
                        var name = row.find('span.xsy_group_members_name').html();

                        $.confirm(rk.i18n('ACCOUNT_DELETE_MEMBER_CONFIRM', name), function() {
                            $.postJson('/json/sns_group/quit.action', {
                                groupId: groupId,
                                uid: uid
                            }).done(function(data) {
                                if (data.status == 0) {
                                    $.msg(rk.i18n('CRM_CORE_OPERATE') + rk.i18n('CRM_CORE_SUCCESS'), 1);
                                    var numLabel = $("label.js_group_member_num");
                                    numLabel.html(parseInt(numLabel.html()) - 1);
                                    row.remove();
                                    afterGroupMemberChange("remove")
                                } else {
                                    $.msg(rk.i18n('CRM_CORE_OPERATE') + rk.i18n('CRM_CORE_FAILED'));
                                }
                            }).fail(function(def, code, message) {
                                $.msg(rk.i18n('REQUEST_ERROR'));
                            })
                        });
                    }
                }
            })

        }
    });
});
